package com.briup.homework.chap08;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * 将学⽣按照成绩保存到集合中，并且名字叫tom的学⽣
 * 不管考多少分都位于班级的第⼀位。
 * 其余学生要按照分数排序（成绩单）
 */
public class Test5_2_ReportCards {
    public static void main(String[] args) {
        // 定义Set集合并传⼊⼀个⾃定义⽐较规则的Comparator
        // 编写代码
        Set<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if("tom".equals(o1.getName())){
                    return -1;
                }else if("tom".equals(o2.getName())){
                    return 1;
                }else {
                    return o2.getScore()-o1.getScore();
                }
            }
        });
        // 测试
        Student student1 = new
                Student("tom", 80);
        Student student5 = new
                Student("tom", 90);
        Student student2 = new
                Student("mike", 90);
        Student student3 = new
                Student("lily", 20);
        Student student4 = new
                Student("chris", 23);
        set.add(student3);
        set.add(student2);
        set.add(student1);
        set.add(student4);
        set.add(student5);
        for (Student student : set) {
            System.out.println(student);
        }
    }
}
class Student {
    private String name;
    private int score;

    public Student(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", score=" + score + "]";
    }
}
